﻿using System;
using System.Diagnostics;
using ChEngine;
using SharpChess.Model;

using UnitTests.ChessLogic;

using Valil.Chess;
using Valil.Chess.Moves;
using Valil.Chess.Pieces.Black;
using Board = ChessKit.ChessLogic.Board;
using Game = Valil.Chess.Game;

namespace PerfHost
{
	class Program
	{
		static void Main(string[] args)
		{
			var board = new Chess.Board();
			TestGetLegalMoves();
			//			TestChengine();
			//      TestValilGetLegalMoves();
		}

		static void TestValilGetLegalMoves()
		{
			while (true)
			{
				var rnd = new Random(0);
				var sw = Stopwatch.StartNew();
				for (int j = 0; j < 120; j++)
				{
					var p = new GameStringDescriptionProxy { Model = new Game() };
					p.LoadNewGame();
					var game = p.Model;
					for (int i = 0; i < 50; i++)
					{
						var moves = game.PossibleMoves;
						if (moves.Count == 0) break;
						var move = moves[rnd.Next(moves.Count)];
						if (move is PromotionMove) ((PromotionMove)move).PromotionType = typeof(BlackQueen);
						game.Make(move);
					}
				}
				sw.Stop();
				Console.WriteLine(sw.Elapsed);
			}
		}

		static void TestGetLegalMoves()
		{
			while (true)
			{
				var rnd = new Random(0);
				var sw = Stopwatch.StartNew();
				var counter = 0;
				for (int j = 0; j < 600; j++)
				{
					var position = Board.StartPosition;
					for (int i = 0; i < 150; i++)
					{
						var moves = position.GetLegalMoves();
						if (moves.Count == 0) break;
						position = position.MakeMove(moves[rnd.Next(moves.Count)]);
						counter++;
					}
				}
				sw.Stop();
				Console.WriteLine("{0}\t- {1:F0}", 
					sw.ElapsedMilliseconds / 1000d, 
					counter / sw.ElapsedMilliseconds);
			}
		}
		static void TestChengine()
		{
			while (true)
			{
				var rnd = new Random(0);
				var sw = Stopwatch.StartNew();
				for (int j = 0; j < 400; j++)
				{
					var position = new ChessBoard();
					for (int i = 0; i < 50; i++)
					{
						var moves = position.GetLegalMoves();
						if (moves.Count == 0) break;
						position.Move(moves[rnd.Next(moves.Count)]);
					}
				}
				sw.Stop();
				Console.WriteLine(sw.Elapsed);
			}
		}


		static void TestCompare()
		{
			var sw = Stopwatch.StartNew();
			//			for (int i = 0; i < 5; i++)
			//			{
			new CompareTest().SearchForBugs(1);
			//			}
			sw.Stop();
			Console.WriteLine(sw.Elapsed);
			//		  Console.ReadKey();
		}
	}
}
